package org.uoss.webapp.framework.struts2.tags.components;

import java.io.IOException;
import java.io.Writer;

import org.apache.struts2.components.Component;
import org.uoss.core.util.ValidationUtil;

import com.opensymphony.xwork2.util.ValueStack;

public final class Struts2PagerComponent extends Component {
	private String totalCount;
	private String pageIndex;
	private String pageSize;
	private String pageURL;

	public Struts2PagerComponent(ValueStack stack) {
		super(stack);
	}

	@Override
	public boolean start(Writer writer) {
		//boolean result = super.start(writer);
		try {
			writer.write(generateHTMLTable().toString());
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		return super.start(writer);
	}

	@SuppressWarnings("unused")
	private StringBuilder generateHTMLDIV() {
		StringBuilder pagingContent = new StringBuilder("<div id='toolbar' style='width: 100%;align: center'>");
		pagingContent.append("<div style='align: center'>");
		if (this.getTotalRecords() != 0) {
			if (this.getFrom() > this.getSize()) {
				pagingContent.append(this.getHTML("First", 1) + "&nbsp;&nbsp;");
			}
			else {
				pagingContent.append("First" + "&nbsp;&nbsp;");
			}

			if (this.getCurrentPageNum() > 1) {
				pagingContent.append(this.getHTML("Previous", this.getCurrentPageNum() - 1) + "&nbsp;&nbsp;");
			}
			else {
				pagingContent.append("Previous" + "&nbsp;&nbsp;");
			}

			pagingContent.append("&nbsp;&nbsp;from <b>" + this.getFrom() + "</b> to <b>" + this.getTo()
					+ "</b> &nbsp;&nbsp;&nbsp;&nbsp;");

			if (this.getCurrentPageNum() < this.getTotalPages()) {
				pagingContent.append(this.getHTML("Next", this.getCurrentPageNum() + 1) + "&nbsp;&nbsp;");
			}
			else {
				pagingContent.append("Next" + "&nbsp;&nbsp;");
			}

			if (this.getTo() < this.getTotalRecords()) {
				pagingContent.append(this.getHTML("Last", this.getTotalPages()) + "&nbsp;&nbsp;");
			}
			else {
				pagingContent.append("Last" + "&nbsp;&nbsp;&nbsp;&nbsp;");
			}
		}
		pagingContent.append("</div>");
		pagingContent.append("<div style='float: right;valign: middle'>");
		pagingContent.append("total: <b>" + this.getTotalRecords() + "</b> records.&nbsp;&nbsp;");
		pagingContent.append("</div>");
		pagingContent.append("</div>");
		return pagingContent;
	}

	private StringBuilder generateHTMLTable() {
		StringBuilder pagingContent = new StringBuilder(
				"<table width='100%' cellspacing='0' paddingspacing='0' bgcolor='#dfe6ff'>");
		pagingContent.append("<tr>");
		pagingContent.append("<td width='20%'></td>");
		pagingContent.append("<td align='center'>");
		if (this.getTotalRecords() != 0) {
			if (this.getFrom() > this.getSize()) {
				pagingContent.append(this.getHTML("First", 1) + "&nbsp;&nbsp;");
			}
			else {
				pagingContent.append("First" + "&nbsp;&nbsp;");
			}

			if (this.getCurrentPageNum() > 1) {
				pagingContent.append(this.getHTML("Previous", this.getCurrentPageNum() - 1) + "&nbsp;&nbsp;");
			}
			else {
				pagingContent.append("Previous" + "&nbsp;&nbsp;");
			}

			pagingContent.append("&nbsp;&nbsp;from <b>" + this.getFrom() + "</b> to <b>" + this.getTo()
					+ "</b> &nbsp;&nbsp;&nbsp;&nbsp;");

			if (this.getCurrentPageNum() < this.getTotalPages()) {
				pagingContent.append(this.getHTML("Next", this.getCurrentPageNum() + 1) + "&nbsp;&nbsp;");
			}
			else {
				pagingContent.append("Next" + "&nbsp;&nbsp;");
			}

			if (this.getTo() < this.getTotalRecords()) {
				pagingContent.append(this.getHTML("Last", this.getTotalPages()) + "&nbsp;&nbsp;");
			}
			else {
				pagingContent.append("Last" + "&nbsp;&nbsp;&nbsp;&nbsp;");
			}
		}
		pagingContent.append("</td>");
		pagingContent.append("<td width='20%' align='right'>");
		pagingContent.append("total: <b>" + this.getTotalRecords() + "</b> records.&nbsp;&nbsp;");
		pagingContent.append("</td>");
		pagingContent.append("</tr>");
		pagingContent.append("</table>");
		return pagingContent;
	}

	public String getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(String totalCount) {
		this.totalCount = totalCount;
	}

	public String getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(String pageIndex) {
		this.pageIndex = pageIndex;
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	public String getPageURL() {
		return pageURL;
	}

	public void setPageURL(String pageURL) {
		this.pageURL = pageURL;
	}

	private int getTotalRecords() {
		if (ValidationUtil.isEmpty(this.getTotalCount()))
			return 0;
		return Integer.valueOf(this.getTotalCount());
	}

	private int getCurrentPageNum() {
		if (ValidationUtil.isEmpty(this.getPageIndex()))
			return 1;
		return Integer.valueOf(this.getPageIndex());
	}

	private int getSize() {
		return Integer.valueOf(this.getPageSize());
	}

	private int getFrom() {
		return (this.getCurrentPageNum() - 1) * this.getSize() + 1;
	}

	private int getTo() {
		int to = this.getFrom() + this.getSize() - 1;
		int total = this.getTotalRecords();
		if (to > total) {
			to = total;
		}
		return to;
	}

	/**
	 * total pages by total records and page size.
	 * 
	 * @return
	 */
	private int getTotalPages() {
		int totalPages = this.getTotalRecords() / this.getSize();
		int mod = this.getTotalRecords() % this.getSize();
		if (mod > 0) {
			totalPages += 1;
		}
		return totalPages;
	}

	private String getHTML(String label, int pageNum) {
		return "<a href='" + this.getPageURL() + "?pageIndex=" + pageNum + "&pageSize=" + this.getPageSize() + "'><b>"
				+ label + "</b></a>";
	}
}
